﻿using System;
using System.Collections.Generic;
using UnityEngine;

public class MBattleSceneController : BattleSceneController
{
    private MGameUser m_MGameUser;
    public  MGameUser gameUser => m_MGameUser; 
    public  MBattleController mBattleController => m_Controller as MBattleController;
    
    
    ///             BattleScene
    ///             /         \
    ///     BattleController BattleUI
    
    private BattleData m_Data;
    public float delayShowResultTime = 1.5f;

    private const string HasGotLoginRewardKey = "HasGotLoginReward";
    static bool HasGotLoginReward
    {
        get
        {
            return PlayerPrefs.GetInt(HasGotLoginRewardKey) > 0;
        }
        set
        {
            PlayerPrefs.SetInt(HasGotLoginRewardKey, value ? 1 : 0);
        }
    }

    void Awake()
    {
        
    }
    
    // Start is called before the first frame update
    void Start()
    {
        m_Controller = GetComponentInChildren<MBattleController>();
        
        var battleDataConfig = Resources.Load<MBattleDataConfig>("Data/DataConfig");

        try
        {
            if (Game.Instance)
            {
                m_MGameUser = MGame.instance.MUser;
                mBattleController.InitBattle(
                    GetBattleData(m_MGameUser, Game.Instance.curLoadLevel, Game.Instance.curMapId, battleDataConfig),
                    m_Controller.myPlayerId, battleDataConfig);
            }
            else
            {
                m_MGameUser = new MGameUser();
                gameUser.Init(MGame.LoadLocalUserData());

                mBattleController.InitBattle(
                    GetBattleData(gameUser, battleController.level, 0, battleDataConfig),
                    m_Controller.myPlayerId,  battleDataConfig);
            }
        }
        catch (Exception e)
        {
            GameDebug.LogException(e);
        }
        
        ShowBattleUI();
        
        PlayBattleMusic();

        if (!HasGotLoginReward)
        {
            DoActionDelay.DelayAction(1, () =>
            {
                MGame.instance.ShowNoticePanel("Welcome!\n You have received 500 coin!", () =>
                {
                    var panel = this.battleSceneNav.GetComponentInChildren<MBattleMainUI>();
                    if (panel)
                    {
                        HasGotLoginReward = true;
                        panel.AddCoin(500);
                    }
                });
               
            });
        }
    }
    
    public void ShowBattleUI()
    { 
        var battleUIDC = this.battleSceneNav.LoadDisplay<MBattleUIViewController>(); 
        var panel = battleUIDC.Init(m_Controller) as MBattleMainUI;
        panel.uiListener = battleUIDC;
        
        m_Controller.battleListener = panel;
    }

    
    GameBattleData GetBattleData(MGameUser user,int level, int mapId,MBattleDataConfig battleDataConfig)
    {
        GameBattleData battleData = new GameBattleData();
        battleData.level = level;

        if (mapId > 0)
        {
            battleData.mapId = mapId;
        }
        else
        {
            if (level > battleDataConfig.loopMaxLevel)
            {
                battleData.mapId = UnityEngine.Random.Range(1, battleDataConfig.loopMaxLevel + 1);// MapId2LoopMapId(level, battleDataConfig.loopMaxLevel);
            }
            else
            {
                battleData.mapId = level;
            }
        }
#if UNITY_EDITOR
        //battleData.mapId = level = 8;
#endif

        bool firstTimeLevelGame = MGame.HasPlayedLevel < level;

        battleData.firstTimeLevelGame = firstTimeLevelGame;

        if(firstTimeLevelGame)
        {
            MGame.HasPlayedLevel = level;
        }
        
        battleData.players = new List<BattlePlayerData>();
        battleData.players.Add(new BattlePlayerData()
        {
            playerId = 0,
            //fiveDimensions = gameUser.fiveDimensions,
        }); 
        battleData.players.Add(new BattlePlayerData()
        {
            playerId = 1,
        }); 
       
        return battleData;
    }
    
}